package com.lgCore.nettyCocurrent;/**
 * Created by by on 2020/11/13 10:35
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.protobuf.ByteString;
import com.lgCore.protoModel.ProtoMsg;
import com.lgCore.util.ByteObjUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * invok异步执行
 *
 * @program: sxreader_rpc
 * @author: ligang
 * @create: 2020-11-13 10:35
 */
public class ServiceInvokCallable implements Callable<ProtoMsg.RpcResponse> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInvokCallable.class);

    private ProtoMsg.Message message;
    public ServiceInvokCallable(ProtoMsg.Message message){
        this.message = message;
    }
    @Override
    public ProtoMsg.RpcResponse call() throws Exception {
        ProtoMsg.RpcResponse.Builder rpcResponse = ProtoMsg.RpcResponse.newBuilder();
        rpcResponse.setClientId(message.getRpcRequest().getClientId());
        rpcResponse.setRequestId(message.getRpcRequest().getRequestId());
        rpcResponse.setIsSucess(true);
        List<Object> objects = (List<Object>) ByteObjUtil.bytesToObject(message.getRpcRequest().getRequestObjects().toByteArray());
//        List<Object> objects = JSONArray.parseArray(message.getRpcRequest().getRequestObjects(), Object.class);
//        rpcResponse.setResponse(JSON.toJSONString(objects.get(0)));
        rpcResponse.setResponse(ByteString.copyFrom(ByteObjUtil.objToBytes(objects.get(0))));
        rpcResponse.setType(ProtoMsg.HeadType.RPC_RESPONSE);
        return rpcResponse.build();
    }
}
